package com.chzc.fm.duotin.adapter.viewpager;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chzc.fm.duotin.bean.home.RecommendDataList;
import com.chzc.fm.duotin.ui.home.HomeFragment;
import com.chzc.fm.duotin.ui.home.utils.JumpManager;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.FileUtils;
import com.chzc.fm.duotin.utils.HttpUtil;
import com.chzc.fm.duotin.utils.LogUtil;

import java.io.File;
import java.util.List;

/**
 * //home页面广告页的适配器
 * Created by Administrator on 2016/6/24.
 */
public class HomeBannerAdapter extends PagerAdapter {

    public static final int FAKE_BANNER_SIZE = 100;
    private static final int DEFAULT_BANNER_SIZE = 6;
    //传入图片的地址
    private List<String>imUrls;
    private List<ImageView> viewList;
    private List<RecommendDataList> dataList = null;
    private Context context;
    public void setDataList(List<RecommendDataList> dataList) {
        this.dataList = dataList;
    }

    public HomeBannerAdapter(){}
    public HomeBannerAdapter(Context context,List<ImageView> viewList, List<String>imUrls){
        this.context = context;
        this.viewList = viewList;
        this.imUrls = imUrls;
    }

    @Override
    public int getCount() {

        //添加循环广告位时所需的设置

        //return FAKE_BANNER_SIZE;

        return HomeFragment.FAKE_BANNER_SIZE;//default
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
    //实例化布局
    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        
        position%=6;

        final ImageView imageView = viewList.get(position);
        container.removeView(imageView);
        container.addView(imageView);
        final String imUrl = imUrls.get(position);
        //下载并设置图片
        new DuotinTask(new DuotinTask.IRequest() {
            @Override
            public Object doRequest() {
                //将图片以文件的形式保存
                File file = HttpUtil.download(imUrl, FileUtils.DIR_IMAGE,FileUtils.getFileNameByHashCode(imUrl),null);
                return file;
            }
        }, new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {
                File file = (File) object;
                Bitmap ivBanner= BitmapFactory.decodeFile(file.getAbsolutePath());
                //int height = ivBanner.getHeight();
               // imageView.setLayoutParams();
                int height = ivBanner.getHeight();
                int width = ViewGroup.LayoutParams.MATCH_PARENT;
                ViewPager.LayoutParams params = new ViewPager.LayoutParams();
                params.width = width;
                params.height = 200;
                imageView.setImageBitmap(ivBanner);
            }

            @Override
            public void error(String msg) {
                LogUtil.e(msg);
            }
        }).execute();

        //由于事件被拦截了所以只能在adapter中获取数据
        final int currItem = position;
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RecommendDataList data = dataList.get(currItem);
                String id = data.getItem_value();
                //传递需要拼接的id过去
                JumpManager.jumpToPlayer1(context,id);
            }
        });

        return imageView;
    }

    //实现第一屏滑动到最后一屏




    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
       // ImageView imageView =viewList.get(position);
        container.removeView((View) object);
    }
}
